const {
	HttpPost,URL,timestampToTime,getNowFormatDate
} = require('utils/utils');


App({
  async onLaunch () {
    await this.appRoute()
  },

	wxLogin(){
		return new Promise(function(reslove,reject){
		  wx.login({
			success (res) {
			 if (res.code) {
         console.log("code", res)
				reslove(res.code);
			 }else{
				console.error('wx.login invalid code. res:', res)
			 }
			},
			fail: (err) => {
			 console.error('wx.login 失败', err)
			},
		  })
		})
	},
	
	async getOpenid(){
		var code = await this.wxLogin();
		let res = await HttpPost('getopenid',{
		   type: 'getopenid',
       appcode: code,
       appname:'hotsearch',
		})
		let {openid,runtype} = res
		this.globalData.openid = openid
		this.globalData.isxan = openid == "o1EEX0ROW2hn6S7M6wygLtgbBRsg"
		this.globalData.runtype = runtype
		return [openid, runtype]
	},

	async appRoute(){
		if (this.globalData){
			return
    }
    this.globalData = {
			runtype:"songshen",
      openId: "",
      name: "微信用户",
      todayCost: 0,
      totalCost: 0,
      today:getNowFormatDate(),

      bkgColor:"#584f60",
      clickColor:"#222226",

      lightBkgColor:"#f6f6f9",
      darkBkgColor:"#584f60",

      fontColor:"#ffffff",

      changeTheme:false,

      robotPlugin:null,
      URL:URL,
			bgimg:"/images/norecord.jpg",
			
			systemInfo: null,
			settings: {
				theme: 'classic',
				showGrid: true,
				showGhost: true,
				isMuted: false,
				sensitivity: 50
			},

  
    };
	  console.log("appRoute globalData", this.globalData)
		wx.onAppRoute((res) => {
			let pages = getCurrentPages()
			let view = pages[pages.length - 1]
			const curPage = pages[pages.length - 1];

			console.log("onAppRoute", view, res, curPage, this.globalData)
	
			if (curPage){
				// 统一设置转发给好友的分享内容
				curPage.onShareAppMessage = () => ({
					path: curPage.route // 默认分享当前页面路径
				});
				// 设置分享到朋友圈内容（需基础库≥2.11.3）
				curPage.onShareTimeline = () => ({
					query: 'from=timeline'
				});
			}
			if(view) {
				view.setData({globalData:this.globalData})
			}
		  })
	},

  // 原理：初始透明，播动画时去除透明  stop中的duration是针对前面一个操作的  可以两个操作后面接一个step，一起播
  showOpacity(that, opacity, text) {
    var animation = wx.createAnimation({
      duration: 800,    // 滑动动画持续时间 ms
      timingFunction: 'ease',
    });
    animation.opacity(opacity).translateY(-20).step().translateY(-20).opacity(0).step().translateY(40).step({duration:0})   // 滑动距离(消失点距离起始点)
    that.setData({
      userAnimation: animation.export(),
      text:text,
    })
  },

  changeTheme(){
    this.globalData.bkgColor = this.globalData.bkgColor == this.globalData.lightBkgColor ? this.globalData.darkBkgColor : this.globalData.lightBkgColor
    this.globalData.changeTheme = true
    this.changeThemeImpl()
  },
  changeThemeImpl(){
    wx.setNavigationBarColor({
      frontColor: this.globalData.fontColor,
      backgroundColor: this.globalData.bkgColor,
    })
    wx.setTabBarStyle({
      color: this.globalData.fontColor,
      // selectedColor: '#00FF00',
      backgroundColor: this.globalData.bkgColor,
      // borderStyle: 'white'
    })
    // wx.setTabBarItem({
    //   index: 0,
    //   text: 'text',
    //   iconPath: '/path/to/iconPath',
    //   selectedIconPath: '/path/to/selectedIconPath'
    // })
	},


  // loginRobot(){
  //   var plugin = requirePlugin("chatbot");

  //   plugin.init({
  //     appid: "VyU9VAgRIxwXeNJ7eqlE5lJvKBBvKf", //微信对话开放平台小程序插件appid
  //     openid: this.globalData.openId, // 小程序用户的openid，必填项
  //     userHeader: "微信用户", // 用户头像,不传会弹出登录框
  //     userName: "微信用户", // 用户昵称,不传会弹出登录框
  //     anonymous: false, // 是否允许匿名用户登录，版本1.2.9后生效, 默认为false，设为true时，未传递userName、userHeader两个字段时将弹出登录框
  //     success: () => {
  //       console.log("loginRobot success")
  //     },//非必填
  //     fail: (error) => {
  //       console.error("loginRobot error", error)
  //     },//非必填
  //   })

  //   this.globalData.robotPlugin = plugin
  // },


});